/**
 * Distributed Retail System
 * Sep 30, 2012 
 */
package comp6231.project.replica.dimitri;

/**
 * @author dimitri.tiago
 *
 * This class implements the semaphore synchronization construct
 */
public class Semaphore 
{
	/**
	 * Semaphore value.
	 */
	private int value;
	
	/**
	 * Instantiate semaphore object and initialize with specified value.
	 * @param value semaphore value.
	 */
	public Semaphore( int value )
	{
		this.value = value;
	}
	
	/**
	 * Wait on thread.
	 */
	public synchronized void Wait()
	{
		while ( value <= 0 )
		{
			try
			{
				wait();
			}
			catch( InterruptedException e )
			{
				; // no-op
			}
		}
		
		value--;
	}
	
	/**
	 * Signal thread.
	 */
	public synchronized void Signal()
	{
		++value;
		notifyAll();
	}
}